<?php
/**
 * Description of ContestFacade
 *
 * @author TheFaSTeR
 */
class ContestFacade 
{
    public static function getContest($id)
    {
        return Contest::model()->findByPk($id);
    }
    
    public static function getActualContests()
    {
        $now = date('Y-m-d H:i:s', time());
        return Contest::model()->findAll("to_date>=:now AND from_date<=:now", array(':now' => $now));
    }

    public static function getActualRandomContest()
    {
        $now = date('Y-m-d H:i:s', time());
        $criteria = new CDbCriteria();
        $criteria->condition = 'to_date>=:now AND from_date<=:now';
        $criteria->params = array(':now' => $now);
        $criteria->order = "rand()";
        $criteria->limit = 1;

        return Contest::model()->find($criteria);
    }
    
    public static function getActualContestsCount()
    {
        $now = date('Y-m-d H:i:s', time());
        return Contest::model()->count("to_date>=:now AND from_date<=:now", array(':now' => $now));
    }
    
    public static function getContestImageById($id)
    {
        $model=ContestImage::model()->findByPk((int)$id);
        if($model===null)
                throw new CHttpException(404,'Страница не найдена');
        return $model;
    }
    
    public static function loadModel($id)
    {
        $model=Contest::model()->findByPk((int)$id);
        if($model===null)
                throw new CHttpException(404,'Страница не найдена');
        return $model;
    }
    
    public static function createContest($data)
    {
        $contest = new Contest();
        $contest->attributes = $data;                       
        $contest->from_date = strtotime($data['from_date']);
        $contest->to_date = strtotime($data['to_date']);
        $contest->save();
        
        return $contest;
    }
    
    public static function getContestUploadsDir(Contest $model)
    {
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/contest");
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/contest/" . $model->id);
        
        return Yii::getPathOfAlias('webroot') . "/uploads/contest/" . $model->id . "/";
    }
    
    public static function getContestImageUploadsDir($contestId)
    {        
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/contest");
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/contest/" . $contestId);
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/contest/" . $contestId . "/images");
        
        return Yii::getPathOfAlias('webroot') . "/uploads/contest/" . $contestId . "/images/";
    }
    
    public static function saveContestImage($uploadImage, Contest $model)
    {
        $oldImageId = $model->image_id;
        $model->image_id = $uploadImage->image_id;   
        $model->from_date = strtotime($model->from_date);
        $model->to_date = strtotime($model->to_date);
        self::saveContestModel($model);
        if ($oldImageId != null)
        {
            UploadImageFacade::unsetImage($oldImageId);
        } 
    }
    
    public static function saveImage($uploadImage, ContestImage $model)
    {                      
        $oldImageId = $model->image_id;
        $model->image_id = $uploadImage->image_id;             
        self::deleteContestImageParticipants($model->id);
        $model->votes = 0;
        self::saveContestImageModel($model);
        if ($oldImageId != null)
        {
            UploadImageFacade::unsetImage($oldImageId);
        }
    }
    
    public static function deleteContestImageParticipants($contestImageId)
    {
        $participants = Contestvote::model()->findAll("contest_image_id=:contestImageId", array(":contestImageId" => $contestImageId));
        if ($participants !== null)
        {
            foreach ($participants as $participant)
            {
                $participant->delete();
            }
        }        
    }
    
    public static function getContestImageByContest($contestId)
    {
        $userId = Yii::app()->user->id;
        $model = ContestImage::model()->find("contest_id=:contestId AND user_id=:userId", array(":contestId" => $contestId, ":userId" => $userId));
        return $model;
    }
    
    public static function getContestImage($contestId)
    {
        $userId = Yii::app()->user->id;        
        $model = ContestImage::model()->find("contest_id=:contestId AND user_id=:userId", array(":contestId" => $contestId, ":userId" => $userId));
        if($model===null)
        {            
            $contestImage = new ContestImage();
            $contestImage->user_id = $userId;
            $contestImage->contest_id = $contestId;
            $contestImage->votes = 0;            
            $contestImage->save();               
            return $contestImage;
        }        
        return $model;
    }
    
    public static function getContestImages($contestId)
    {
        return ContestImage::model()->findAll("contest_id=:contestId", array(":contestId" => $contestId));
    }
    
    public static function getContestImagesCount($contestId)
    {
        return ContestImage::model()->count("contest_id=:contestId", array(":contestId" => $contestId));
    }
    
    public static function saveContestModel(Contest $model)
    {
        $model->save();
    }
    
    public static function saveContestImageModel(ContestImage $model)
    {
        $model->save();
    }
    
    public static function isCanVote($contestImageId, $userId)
    {
        $result = Contestvote::model()->find("contest_image_id=:contestImageId AND user_id=:userId", array(
            ":contestImageId"   => $contestImageId,
            ":userId"           => $userId
        ));
        
        if ($result === null)
        {
            return true;
        }
        return false;
    }
    
    public static function setVote($contestImageId, $userId)
    {
        $contestImage = self::getContestImageById($contestImageId);
        $contestImage->votes = $contestImage->votes + 1;
        $contestImage->save();
        
        $vote = new Contestvote();
        $vote->contest_image_id = $contestImageId;
        $vote->user_id = $userId;
        $vote->contest_id = $contestImage->contest_id;
        $vote->save();                
    }
    
    public static function isAlreadyVote($contestImageId, $userId)
    {
        $contestImage = self::getContestImageById($contestImageId);
        
        $result = Contestvote::model()->find("contest_id=:contestId AND user_id=:userId", array(
            ":contestId"    => $contestImage->contest_id,
            ":userId"       => $userId
        ));
        
        if ($result === null)
        {
            return false;
        }
        return true;
    }

}

?>